{{!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}
{{> AutoGenerated}}

package {{struct:javaPackage}};

import com.facebook.swift.codec.*;
import com.facebook.swift.codec.ThriftField.Requiredness;
import com.facebook.swift.codec.ThriftField.Recursiveness;
import java.util.*;
import org.apache.thrift.*;
import org.apache.thrift.async.*;
import org.apache.thrift.server.*;
import org.apache.thrift.transport.*;
import org.apache.thrift.protocol.*;

import static com.google.common.base.MoreObjects.toStringHelper;

@SwiftGenerated
@ThriftUnion("{{struct:name}}"){{> common/ObjectCustomAnnotation}}
public final class {{struct:javaCapitalName}} {
    private static final TStruct STRUCT_DESC = new TStruct("{{struct:name}}");
    private static final Map<String, Integer> NAMES_TO_IDS = new HashMap();
    private static final Map<Integer, TField> FIELD_METADATA = new HashMap<>();

    {{#struct:fields}}
    public static final int _{{field:javaAllCapsName}} = {{field:key}};
    private static final TField {{field:javaTFieldName}} = new TField("{{field:javaName}}", TType.{{#field:type}}{{> TType}}{{/field:type}}, (short){{field:key}});
    {{/struct:fields}}

    static {
      {{#struct:fields}}
      NAMES_TO_IDS.put("{{field:javaName}}", {{field:key}});
      FIELD_METADATA.put({{field:key}}, {{field:javaTFieldName}});
      {{/struct:fields}}
    }

    private Object value;
    private short id;

    {{> union/UnionBuilder}}

    {{^struct:unionFieldTypeUnique?}}{{!
      TODO: remove unionFieldTypeUnique related check and use builder pattern
      If union fields are not unique, we cannot generate thrift constructors,
      therefore setters are needed.
      }}{{> union/UnionSetter}}{{!
    }}{{/struct:unionFieldTypeUnique?}}
    {{#struct:fields}}

    {{> ThriftFieldAnnotation}}

    public {{#field:type}}{{> Type}}{{/field:type}} {{> GetterName}}() {
        if (this.id != {{field:key}}) {
            throw new IllegalStateException("Not a {{field:name}} element!");
        }
        return ({{> FieldType}}) value;
    }

    public boolean isSet{{field:javaCapitalName}}() {
        return this.id == {{field:key}};
    }
    {{/struct:fields}}

    @ThriftUnionId
    public short getThriftId() {
        return this.id;
    }

    public String getThriftName() {
        TField tField = (TField) FIELD_METADATA.get((int) this.id);
        if (tField == null) {
            return "null";
        } else {
            return tField.name;
        }
    }

    public void accept(Visitor visitor) {
    {{#struct:fields}}
        if (isSet{{field:javaCapitalName}}()) {
            visitor.visit{{field:javaCapitalName}}({{> GetterName}}());
            return;
        }
    {{/struct:fields}}
    }

    @Override
    public String toString() {
        return toStringHelper(this)
            .add("value", value)
            .add("id", id)
            .add("name", getThriftName())
            .add("type", value == null ? "<null>" : value.getClass().getSimpleName())
            .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        {{struct:javaCapitalName}} other = ({{struct:javaCapitalName}})o;

        return Objects.equals(this.id, other.id)
                && Objects.deepEquals(this.value, other.value);
    }

    @Override
    public int hashCode() {
        return Arrays.deepHashCode(new Object[] {
            id,
            value,
        });
    }

    public interface Visitor {
    {{#struct:fields}}
        void visit{{field:javaCapitalName}}({{> FieldType}} {{field:javaName}});
    {{/struct:fields}}
    }

    {{> union/UnionWrite}}

    {{> union/UnionRead}}

}
